package edu.guigu.serverservice.remoteaidl;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import androidx.annotation.NonNull;

// AIDL只可传递它所认识的基本数据类型和经过Parcelable序列化的数据
// 要我们来手动进行 “打包” 和 “解包” 的工作
public class Student implements Parcelable {
    private String name;
    private String grade;

    public Student(String name, String grade) {
        this.name = name;
        this.grade = grade;
    }

    public Student() {
    }

    protected Student(Parcel in) {
        name = in.readString();
        grade = in.readString();
    }

    // 用来解包
    public static final Creator<Student> CREATOR = new Creator<Student>() {
        // 对象
        @Override
        public Student createFromParcel(Parcel in) {
            Log.d("TAG", getClass().getSimpleName() + " createFromParcel: 解包处理");
            return new Student(in);
        }

        // 对象数组
        @Override
        public Student[] newArray(int size) {
            Log.d("TAG", getClass().getSimpleName() + " newArray: 解包处理");
            return new Student[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    // 打包(有序的)
    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(grade);
        Log.d("TAG", getClass().getSimpleName() + " writeToParcel: 打包处理");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", grade='" + grade + '\'' +
                '}';
    }
}
